import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/routes/account/view/xindong_edit_text_view.dart';
import 'package:xindong/routes/common/xindong_input_text_multi_line_yemian.dart';
import 'package:xindong/routes/common/xindong_input_text_yemian.dart';
import 'package:xindong/routes/user/auth/xindong_auth_voice_yemian.dart';
import 'package:xindong/routes/user/profile/xindong_profile_ctr.dart';
import 'package:xindong/routes/user/profile/xindong_profile_tags_yemian.dart';
import 'package:xindong/routes/user/profile/view/xindong_profile_edit_text_widget.dart';
import 'package:xindong/routes/user/profile/view/xindong_profile_section_column_widget.dart';
import 'package:xindong/routes/user/profile/view/xindong_profile_section_none_widget.dart';
import 'package:xindong/routes/user/profile/view/xindong_profile_section_widget.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_app_bar_text_button.dart';
import 'package:xindong/widgets/dialog/xindong_birthday_sheet.dart';
import 'package:xindong/widgets/dialog/xindong_cities_sheet.dart';
import 'package:xindong/widgets/dialog/xindong_profile_select_sheet.dart';
import 'package:xindong/widgets/dialog/xindong_selector2_sheet.dart';
import 'package:xindong/widgets/dialog/xindong_stature_sheet.dart';
import 'package:xindong/widgets/xindong_avatar_upload_widget.dart';
import 'package:xindong/widgets/xindong_tags_view.dart';

class XinDongProfileYeMian extends StatefulWidget {
  const XinDongProfileYeMian({super.key});

  @override
  State<XinDongProfileYeMian> createState() => _XinDongProfileYeMianState();
}

class _XinDongProfileYeMianState extends State<XinDongProfileYeMian> {
  final controller = Get.put(XinDongProfileCtr());
  static const TextStyle contentStyle = TextStyle(color: DZColors.contentColor, fontSize: 15);
  static const TextStyle placeholderStyle = TextStyle(color: DZColors.placeholderColor, fontSize: 15);

  @override
  void initState() {
    super.initState();

    controller.requestGetConfig();
    controller.requestGetOccupation();
  }

  void _onTap({required String title}) {
    switch (title) {
      case '昵称':
        {
          XinDongPageSingleton.instance.pushYeMian(XinDongInputTextYeMian(
            inputEnum: XinDongInputProfileEnum.name,
            value: controller.mineUser.name,
            doneCallback: (String text) {
              controller.mineUser.name = text;
              controller.update();
            },
          ));
        }
        break;

      case '语音签名':
        {
          XinDongPageSingleton.instance.pushYeMian(const XinDongAuthVoiceYeMian());
        }
        break;
      case '性别':
        {
          showToast("注册后不支持修改！");
        }
        break;
      case '生日':
        XinDongBirthdaySheet.show(
          isMan: controller.mineUser.sex == Gender.male,
          selectedDateText: controller.mineUser.birthday,
          doneCallback: (text) {
            controller.mineUser.birthday = text;
            controller.update();
          },
        );
        break;
      case '身高':
      case '体重':
        XinDongStatureSheet.show(
          gender: controller.mineUser.sex ?? Gender.none,
          height: controller.mineUser.statureText,
          weight: controller.mineUser.weightText,
          doneCallback: (height, weight) {
            controller.mineUser.stature = height.replaceAll("cm", "");
            controller.mineUser.weight = weight.replaceAll("kg", "");
            controller.update();
          },
        );
        break;
      case '情感状态':
        XinDongProfileSelectSheet.show(
          type: XinDongProfileSelectType.emotion,
          title: title,
          selectText: controller.mineUser.emotion,
          doneCallback: (text) {
            controller.mineUser.emotion = text;
            controller.update();
          },
        );
        break;

      case '学历':
        XinDongProfileSelectSheet.show(
          type: XinDongProfileSelectType.education_background,
          title: title,
          selectText: controller.mineUser.education_background,
          doneCallback: (text) {
            controller.mineUser.education_background = text;
            controller.update();
          },
        );
        break;

      case '年收入':
        XinDongProfileSelectSheet.show(
          type: XinDongProfileSelectType.monthly_salary,
          title: title,
          selectText: controller.mineUser.monthly_salary,
          doneCallback: (text) {
            controller.mineUser.monthly_salary = text;
            controller.update();
          },
        );
        break;
      case "职业":
        XinDongProfileSelectSheet.show(
          type: XinDongProfileSelectType.occupation,
          title: title,
          selectText: controller.mineUser.occupation,
          doneCallback: (text) {
            controller.mineUser.occupation = text;
            controller.update();
          },
        );
        break;

      case "毕业院校":
        XinDongPageSingleton.instance.pushYeMian(XinDongInputTextYeMian(
          inputEnum: XinDongInputProfileEnum.school_of_graduation,
          value: controller.mineUser.school_of_graduation,
          doneCallback: (String text) {
            controller.mineUser.school_of_graduation = text;
            controller.update();
          },
        ));
        break;
      case "家乡":
        XinDongCitiesSheet.show(
          selectText: controller.mineUser.through_membership,
          onDone: (String text) {
            controller.mineUser.through_membership = text;
            controller.update();
          },
        );
        break;

      case "居住情况":
        XinDongProfileSelectSheet.show(
          type: XinDongProfileSelectType.live_status,
          title: title,
          selectText: controller.mineUser.live_status,
          doneCallback: (text) {
            controller.mineUser.live_status = text;
            controller.update();
          },
        );
        break;
      case "是否购房":
        XinDongProfileSelectSheet.show(
          type: XinDongProfileSelectType.is_house_purchase,
          title: title,
          selectText: controller.mineUser.is_house_purchase,
          doneCallback: (text) {
            controller.mineUser.is_house_purchase = text;
            controller.update();
          },
        );
        break;
      case "是否购车":
        XinDongProfileSelectSheet.show(
          type: XinDongProfileSelectType.is_car_purchase,
          title: title,
          selectText: controller.mineUser.is_car_purchase,
          doneCallback: (text) {
            controller.mineUser.is_car_purchase = text;
            controller.update();
          },
        );
        break;
      case "个性签名":
        XinDongPageSingleton.instance.pushYeMian(XinDongInputTextMultiLineYeMian(
          title: title,
          placeholder: '编辑您的个性签名',
          value: controller.mineUser.slogan,
          doneCallback: (String text) {
            controller.mineUser.slogan = text;
            controller.update();
          },
        ));
        break;
      case "自我介绍":
        XinDongPageSingleton.instance.pushYeMian(XinDongInputTextMultiLineYeMian(
          title: title,
          placeholder: '编辑您的自我介绍',
          value: controller.mineUser.self_slogan,
          doneCallback: (String text) {
            controller.mineUser.self_slogan = text;
            controller.update();
          },
        ));
        break;
      case "我的标签":
        XinDongPageSingleton.instance.pushYeMian(XinDongProfileTagsYeMian(
          tags: controller.mineUser.tags,
          onUpdate: (List<String> tags) {
            controller.mineUser.tags = tags;
            controller.update();
          },
        ));
        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar(
        "编辑资料",
        actions: [
          XinDongAppBarTextButton(
            name: '保存',
            onTap: () => controller.requestUserUpdate(),
          )
        ],
      ),
      body: GetBuilder<XinDongProfileCtr>(builder: (profile) {
        final slogan = TextUtil.isEmpty(profile.mineUser.slogan) ? "添加个性签名，交友成功率提高2倍" : "${profile.mineUser.slogan}";
        final selfSlogan =
            TextUtil.isEmpty(profile.mineUser.self_slogan) ? "介绍一下我自己" : "${profile.mineUser.self_slogan}";
        return ListView(
          children: [
            Container(
              alignment: Alignment.center,
              padding: const EdgeInsets.only(top: 23),
              child: XinDongAvatarUploadWidget(
                avatarFile: profile.avatarFile,
                avatarUrl: profile.mineUser.head_100,
                callBack: (file) {
                  profile.requestUpdateAvatar(file);
                },
              ),
            ),
            const SizedBox(height: 24),
            GetBuilder<XinDongUserController>(builder: (controller) {
              return _buildSection(title: '语音签名', content: '', placeholder: '去录制', reward: '录语音签名+1金币');
            }),
            const XinDongProfileSectionNoneWidget(title: '基本资料'),
            _buildItem(title: '昵称', content: profile.mineUser.name, placeholder: '请填写'),
            _buildItem(title: '性别', content: profile.mineUser.sex, placeholder: '请选择'),
            _buildItem(title: '生日', content: profile.mineUser.birthday, placeholder: '待完善'),
            _buildItem(title: '所在地', content: profile.mineUser.city, placeholder: '待完善'),
            _buildItem(title: '身高', content: profile.mineUser.statureText, placeholder: '待完善'),
            _buildItem(title: '体重', content: profile.mineUser.weightText, placeholder: '待完善'),
            _buildItem(title: '情感状态', content: profile.mineUser.emotion, placeholder: '待完善'),
            _buildItem(title: '学历', content: profile.mineUser.education_background, placeholder: '待完善'),
            _buildItem(title: '年收入', content: profile.mineUser.monthly_salary, placeholder: '待完善'),
            _buildItem(title: '职业', content: profile.mineUser.occupation, placeholder: '待完善'),
            XinDongProfileSectionColumnWidget(
              title: '个性签名',
              child: Container(
                alignment: Alignment.topLeft,
                padding: const EdgeInsets.only(top: 8),
                child: Text(
                  slogan,
                  style: TextUtil.isEmpty(profile.mineUser.slogan) ? placeholderStyle : contentStyle,
                ),
              ),
              onTap: () => _onTap(title: '个性签名'),
            ),
            const XinDongProfileSectionNoneWidget(title: '更多资料'),
            _buildItem(title: '毕业院校', content: profile.mineUser.school_of_graduation, placeholder: '待完善'),
            _buildItem(title: '家乡', content: profile.mineUser.through_membership, placeholder: '待完善'),
            _buildItem(title: '居住情况', content: profile.mineUser.live_status, placeholder: '待完善'),
            _buildItem(title: '是否购房', content: profile.mineUser.is_house_purchase, placeholder: '待完善'),
            _buildItem(title: '是否购车', content: profile.mineUser.is_car_purchase, placeholder: '待完善'),
            XinDongProfileSectionColumnWidget(
              title: '自我介绍',
              reward: '完善自我介绍+1金币',
              child: Container(
                alignment: Alignment.topLeft,
                padding: const EdgeInsets.only(top: 8),
                child: Text(
                  selfSlogan,
                  style: TextUtil.isEmpty(profile.mineUser.self_slogan) ? placeholderStyle : contentStyle,
                ),
              ),
              onTap: () => _onTap(title: '自我介绍'),
            ),
            XinDongProfileSectionColumnWidget(
              title: '我的标签',
              child: Container(
                alignment: Alignment.topLeft,
                padding: const EdgeInsets.only(top: 10),
                child: XinDongTagsView(dataList: profile.mineUser.tags),
              ),
              onTap: () => _onTap(title: '我的标签'),
            ),
            XinDongProfileSectionNoneWidget(title: '手机号', content: profile.mineUser.phone, removeBottom: false),
            const SizedBox(height: 16),
          ],
        );
      }),
    );
  }

  Widget _buildSection({required String title, required String? content, required String placeholder, String? reward}) {
    return XinDongProfileSectionWidget(
      title: title,
      content: content,
      placeholder: placeholder,
      reward: reward,
      onTap: () => _onTap(title: title),
    );
  }

  Widget _buildItem({required String title, required String? content, required String placeholder}) {
    return XinDongProfileEditTextWidget(
      title: title,
      content: content,
      hint: placeholder,
      onTap: () => _onTap(title: title),
    );
  }
}
